package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.SignRecordConService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LEARN_SECTION;

@Component
@Slf4j
@RequiredArgsConstructor
public class listenWriteReplyMessage {
    private final SignRecordConService signRecordConService;

    /**
     * 监听到问答回复消息
     *
     * @param userId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.point.quene", durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    ))
    public void listenWriteReplyMessage(Long userId) {
        log.info("监听到问答回复消息，userId:{}", userId);
        signRecordConService.addPointRecord(userId, 5, PointsRecordType.QA);
    }
    /**
     * 监听到签到成功消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sign.point.quene", durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    ))
    public void listenSignMessage(SignInMessage message) {
        log.info("监听到签到消息，message:{}", message);
        signRecordConService.addPointRecord(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }
    /**
     * 监听到学习完成消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "learning.point.quene", durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =LEARN_SECTION
    ))
    public void listenLearningMessage(Long userId) {
        log.info("监听到学习完成消息，message:{}", userId);
        signRecordConService.addPointRecord(userId,10, PointsRecordType.LEARNING);
    }

}
